package com.sskj.fabi.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.bean.PublishBean;
import com.sskj.fabi.presenter.FabiBuyAndSellActivityPresenter;
import com.sskj.fabi.util.TipFabiUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CommonUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.FABI_BUY_AND_SELL)
public class FabiBuyAndSellActivity extends BaseActivity<FabiBuyAndSellActivityPresenter> {
    @BindView(R2.id.tvType)
    TextView tvType;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Autowired
    boolean isBuy = false;
    private BottomSheetDialog typeSheet;
    private MVCCoolHelper<List<PublishBean>> mvcHelper;
    private MaterialDialog tipCancel;
    private HashMap<String, String> statusMap;

    @Override
    protected int getLayoutResId() {
        return R.layout.fabi_activity_fabi_buy_and_sell;
    }

    @Override
    public FabiBuyAndSellActivityPresenter getPresenter() {
        return new FabiBuyAndSellActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        tvType.setText(isBuy ? "购买记录" : "出售记录");
        statusMap = new HashMap<>();
        statusMap.put("1", "撤单");
        statusMap.put("2", "已完成");
        statusMap.put("3", "已撤单");
        ClickUtil.click(tvType, () -> {
            if (typeSheet == null) {
                typeSheet = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, position, v) -> {
                    typeSheet.dismiss();
                    if (!isBuy && position == 0) {
                        isBuy = true;
                        tvType.setText("购买记录");
                    } else if (isBuy && position == 1) {
                        isBuy = false;
                        tvType.setText("出售记录");
                    }
                    mvcHelper.refresh();

                }, "购买记录", "出售记录");
            }
            typeSheet.show();
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 10))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.fabi_f2f))
        );
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.fabi_recy_item_buy_and_sell, new SlimInjector<PublishBean>() {
            @Override
            public void onInject(PublishBean data, IViewInjector injector, List payloads) {

                injector
                        .visibility(R.id.viewYL, data.getPayType().contains("3") ? View.VISIBLE : View.GONE)
                        .visibility(R.id.viewWX, data.getPayType().contains("1") ? View.VISIBLE : View.GONE)
                        .visibility(R.id.viewAL, data.getPayType().contains("2") ? View.VISIBLE : View.GONE)
                        .text(R.id.tvPrice, CommonUtil.dealCNY(data.getPrice()))
                        .text(R.id.tvNum, CommonUtil.dealCoin(data.getDealsNum()))
                        .text(R.id.tvMoney, CommonUtil.dealCoin(new BigDecimal(data.getDealsNum()).subtract(new BigDecimal(data.getTransNum())).toPlainString()))
                        .text(R.id.tvMin, data.getMinPrice())
                        .text(R.id.tvMax, data.getMaxPrice())
                        .text(R.id.btCancel, statusMap.get(data.getStatus()))
                        .text(R.id.tvTime, String.format("创建时间：%s", data.getCreateTime()))
                        .textColor(R.id.btCancel, ContextCompat.getColorStateList(App.INSTANCE, isBuy ? R.color.fabi_selector_ff3 : R.color.fabi_selector_05c))
                        .background(R.id.btCancel, ContextCompat.getDrawable(App.INSTANCE, isBuy ? R.drawable.fabi_selector_ff3 : R.drawable.fabi_selector_05c))
                        .enable(R.id.btCancel, data.getStatus().equals("1"))
                        .clicked(R.id.btCancel, v -> {
                            if (tipCancel == null) {
                                tipCancel = TipFabiUtil.showCancel(FabiBuyAndSellActivity.this, () -> {
                                    tipCancel.dismiss();
                                    mPresenter.cancelPublishOrder(isBuy, data.getId() + "");
                                });
                            } else {
                                tipCancel.show();
                            }

                        })
                ;
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<PublishBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<PublishBean>)
                page -> mPresenter.getData(page, 20, isBuy), 20);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    public void cancelSuccess() {
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }
}
